package com.example.everying.service.chat;

import com.alibaba.fastjson2.JSONObject;
import com.example.everying.constant.CacheConstants;
import com.example.everying.model.dto.system.NotificationMessage;
import com.example.everying.model.entity.official.OfficialNotification;
import com.example.everying.model.entity.system.SysNotification;
import com.example.everying.service.official.OfficialNotificationService;
import com.example.everying.service.system.SysNotificationService;
import com.example.everying.service.user.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Arrays;

import static org.apache.kafka.common.requests.DeleteAclsResponse.log;

@Service
@RequiredArgsConstructor
public class WebSocketBroadcastService {

    private final RedisTemplate<String, Object> redisTemplate; // 使用配置好的RedisTemplate
    private final ObjectMapper objectMapper;
    @Autowired
    private SysNotificationService sysNotificationService;

    @Autowired
    private OfficialNotificationService officialNotificationService;

    @Autowired
    private UserService userService;


    /**
     * 向所有节点广播系统通知
     */
    public void broadcastSystemNotification(String id) {
        SysNotification notification = sysNotificationService.getById(id);

        NotificationMessage message = new NotificationMessage();
        message.setTitle("系统通知");
        message.setContent(notification.getContent());
        message.setType(NotificationMessage.MessageType.SYSTEM);

//        String json = objectMapper.writeValueAsString(message);
//        byte[] payload = json.getBytes(StandardCharsets.UTF_8); // 强制转为 byte[]

        log.info("发送Redis消息到频道{}，内容：{}", CacheConstants.NOTIFICATION_KEY, message);
        redisTemplate.convertAndSend(CacheConstants.NOTIFICATION_KEY, message); // 发送 byte[]

        // 修改通知状态
        notification.setStatus("1");
        sysNotificationService.updateById(notification);
    }

    /**
     * 向所有节点广播官方通知
     */
    public void broadcastOfficialNotification(String id) {
        OfficialNotification notification = officialNotificationService.getById(id);

        NotificationMessage message = new NotificationMessage();
        message.setTitle("官方通知");
        message.setContent(notification.getContent());
        message.setType(NotificationMessage.MessageType.OFFICIAL);

//        String json = objectMapper.writeValueAsString(message);
//        byte[] payload = json.getBytes(StandardCharsets.UTF_8); // 强制转为 byte[]

        log.info("发送Redis消息到频道{}，内容：{}", CacheConstants.OFFICIAL_NOTIFICATION_KEY, message);
        redisTemplate.convertAndSend(CacheConstants.OFFICIAL_NOTIFICATION_KEY, message); // 发送 byte[]

        // 修改通知状态
        notification.setStatus("1");
        notification.setUpdateTime(LocalDateTime.now());
        officialNotificationService.updateById(notification);

        // 更新用户未读数
        userService.noticeUnreadCountPlus();
        broadcastNoticeUnreadCount();

    }

    /**
     * 向所有节点广播官方通知
     */
    public void broadcastNoticeUnreadCount() {

        NotificationMessage message = new NotificationMessage();
        message.setTitle("官方通知未读状态");
        message.setContent("1");
        message.setType(NotificationMessage.MessageType.NOTICE_UNREAD_STATUS);

//        String json = objectMapper.writeValueAsString(message);
//        byte[] payload = json.getBytes(StandardCharsets.UTF_8); // 强制转为 byte[]

        log.info("发送Redis消息到频道{}，内容：{}", CacheConstants.NOTICE_UNREAD_STATUS_KEY, message);
        redisTemplate.convertAndSend(CacheConstants.NOTICE_UNREAD_STATUS_KEY, message); // 发送 byte[]


    }





    /**
     * 向特定用户组广播消息
     */
    public void broadcastToGroup(String groupId, String message) {
        JSONObject payload = new JSONObject();
        payload.put("type",  "group_message");
        payload.put("groupId",  groupId);
        payload.put("content",  message);

        redisTemplate.convertAndSend("sys.notification",  payload);
    }
}
